package com.detech.sbmh.business.login.serviceimpl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.detech.sbmh.base.hibernate.user.dao.TbSysLogininforDAO;
import com.detech.sbmh.base.hibernate.user.entity.TbSysLogininfor;
import com.detech.sbmh.base.page.Page;
import com.detech.sbmh.base.page.PageDAO;
import com.detech.sbmh.business.login.service.ISysLogininforService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;

@Service
public class SysLogininforServiceImpl implements ISysLogininforService {

    @Resource
    public TbSysLogininforDAO tbSysLogininforDAO;
    @Resource
    private PageDAO pageDAO;

    @Override
    public Page<TbSysLogininfor> selectPageLogininforList(Page page, String ipaddr, String status, String userName, String msg, String accessTime, String deviceType) {
        String hql = "from TbSysLogininfor t where 1=1 ";
        if(StrUtil.isNotBlank(ipaddr)){
            hql = hql + " and t.ipaddr = ? ";
            page.addParam(ipaddr);
        }
        if(StrUtil.isNotBlank(msg)){
            hql = hql + " and t.msg like ? ";
            page.addParam("%"+msg+"%");
        }
        if(StrUtil.isNotBlank(status)){
            hql = hql + " and t.status = ? ";
            page.addParam(status);
        }
        if(StrUtil.isNotBlank(deviceType)){
            hql = hql + " and t.deviceType = ? ";
            page.addParam(deviceType);
        }
        if(StrUtil.isNotBlank(userName)){
            hql = hql + " and t.username = ? ";
            page.addParam(userName);
        }
        if(StrUtil.isNotBlank(accessTime)){
            hql = hql + " and t.accessTime >= ? and t.accessTime < ?+1 ";
            Date logTimeDate = DateUtil.parse(accessTime,"yyyy-MM-dd").toJdkDate();
            page.addParam(logTimeDate);
            page.addParam(logTimeDate);
        }
        hql = hql + " order by t.accessTime desc ";
        page.setHql(hql);
        return pageDAO.queryDataList(page);
    }

    /**
     * 新增系统登录日志
     *
     * @param logininfor 访问日志对象
     */
    @Override
    public void insertLogininfor(TbSysLogininfor logininfor) {
        tbSysLogininforDAO.save(logininfor);
    }

    /**
     * 查询操作日志详细
     *
     * @param infoId 操作ID
     * @return 操作日志对象
     */
    @Override
    public TbSysLogininfor selectLogininforById(Long infoId) {
        return tbSysLogininforDAO.findById(infoId);
    }

}
